package com.party.mobile.web.controller.enterpriseservice;

import com.google.common.base.Strings;
import com.party.common.paging.Page;
import com.party.common.utils.DateUtils;
import com.party.common.utils.PartyCode;
import com.party.core.exception.BusinessException;
import com.party.core.model.enterpriseservice.EpService;
import com.party.core.service.enterpriseservice.IEpServiceService;
import com.party.mobile.web.dto.AjaxResult;
import org.apache.commons.lang3.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @program: party
 * @description: 企业服务控制器
 * @author: Hua Qunhao
 * @create: 2019-01-08 16:45
 */
@Controller
@RequestMapping(value = "/epservice")
public class EnterpriseServiceController {

    @Autowired
    private IEpServiceService epServiceService;


    Logger logger = LoggerFactory.getLogger(EnterpriseServiceController.class);

    /**
     * 获取服务列表
     *
     * @return 服务列表
     */
    //@Authorization
    @ResponseBody
    @RequestMapping(value = "/list")
    public AjaxResult list(HttpServletRequest request, Page page, String categoryId) {
        page.setLimit(10);
        if (Strings.isNullOrEmpty(categoryId)) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "参数不合法");
        }
        try {
            List<Map<String, Object>> datalist = new ArrayList<>();
            EpService epService = new EpService();
            epService.setCategoryId(categoryId);

            List<EpService> epServiceList = epServiceService.listPage(epService, page);

            for (EpService epS : epServiceList) {

                Map<String, Object> mmp = new HashMap<>();
                mmp.put("id", epS.getId());
                mmp.put("title", epS.getTitle());
                mmp.put("subTitle", epS.getSubTitle());
                mmp.put("pic", epS.getPic());
                datalist.add(mmp);
            }
            return AjaxResult.success(datalist, page);
        } catch (BusinessException be) {
            logger.error("获取服务列表异常", be);
            return AjaxResult.error(be.getCode(), be.getMessage());
        }
    }

    /**
     * 获取服务详情
     *
     * @param serviceId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/serviceInfo")
    public AjaxResult epServiceDetail(HttpServletRequest request,String serviceId) {
        if (Strings.isNullOrEmpty(serviceId)) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "参数不合法");
        }
        try {
            Map<String,Object> mmp = new HashMap<>();
            EpService epService = epServiceService.get(serviceId);
            if(null!=epService){
                mmp.put("id",epService.getId());
                mmp.put("title",epService.getTitle());
                mmp.put("createDate",DateUtils.formatDate(epService.getCreateDate(),"yyyy-MM-dd HH:mm"));
                mmp.put("content",StringEscapeUtils.unescapeHtml4(epService.getContent()));
                mmp.put("pic",epService.getPic());
                mmp.put("subTitle",epService.getSubTitle());
                mmp.put("url",epService.getUrl());
            }else{
                throw new BusinessException("数据不存在");
            }
            return AjaxResult.success(mmp);
        } catch (BusinessException be) {
            logger.error("获取服务列表异常", be);
            return AjaxResult.error(be.getCode(), be.getMessage());
        }
    }
}
